package com.oceantrader.sale.biz.hello.controller;

import java.util.Map;
import java.util.List;

import org.springframework.stereotype.Component;

import com.oceantrader.sale.commons.config.api.ApiResult;
import com.oceantrader.sale.commons.config.exceptions.ServiceException;
import com.oceantrader.sale.biz.hello.entity.Hello;
import com.oceantrader.sale.biz.hello.service.HelloService;
import com.xuesinuo.xtool.Np;

import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class HelloController {

    private final Router router;
    private final HelloService studentService;

    @PostConstruct
    public void start() {
        getHello();
        addHello();
        delHello();
        setHello();
    }

    /**
     * 查询Hello
     */
    private void getHello() {
        router.route(HttpMethod.GET, "/backend/hello/:id").handler(http -> {// 进入route，有一个务必要执行的任务，执行rc.next()
            String id = http.pathParam("id");
            Hello param = new Hello();
            param.setId(Long.valueOf(id));
            studentService.getList(param)
                    .onSuccess(helloList -> {
                        // 不要使用任何阻塞API
                        // 方案1：使用Vertx自带的SDK，不阻塞
                        // 方案2：Http可以使用route.blockingHandler()
                        // 方案3：使用vertx.executeBlocking()
                        http.response().write(ApiResult.ok(Map.of("helloList", helloList)));
                    })
                    .onFailure(e -> http.fail(e))
                    .onComplete(helloList -> http.next());
        });
    }

    /**
     * 新增Hello
     */
    private void addHello() {
        router.route(HttpMethod.POST, "/backend/hello/add").handler(http -> {
            String body = http.body().asString();
            Hello param = Np.i(Json.decodeValue(body, Hello.class)).o(new Hello());
            studentService.add(param)
                    .onSuccess(newId -> {
                        http.response().write(ApiResult.ok(Map.of("id", newId)));
                    })
                    .onFailure(e -> http.fail(e))
                    .onComplete(newId -> http.next());
        });
    }

    @Data
    public static class DeleteHelloParam {
        private List<Long> idList;
    }

    /**
     * 删除Hello
     */
    private void delHello() {
        router.route(HttpMethod.POST, "/backend/hello/del").handler(http -> {
            String body = http.body().asString();
            DeleteHelloParam param = Np.i(Json.decodeValue(body, DeleteHelloParam.class)).o(new DeleteHelloParam());
            if (param.getIdList() == null || param.getIdList().isEmpty()) {
                throw new ServiceException("idList can not be empty");
            }
            studentService.del(param.getIdList())
                    .onSuccess(count -> {
                        http.response().write(ApiResult.ok(Map.of("count", count)));
                    })
                    .onFailure(e -> http.fail(e))
                    .onComplete(count -> http.next());
        });
    }

    /**
     * 修改Hello
     */
    private void setHello() {
        router.route(HttpMethod.POST, "/backend/hello/set").handler(http -> {
            Hello param = http.body().asPojo(Hello.class);
            studentService.set(param)
                    .onSuccess(count -> {
                        http.response().write(ApiResult.ok(Map.of("count", count)));
                    })
                    .onFailure(e -> http.fail(e))
                    .onComplete(count -> http.next());
        });
    }
}
